﻿using Microsoft.AspNetCore.Mvc;
using YL.NetCore.NetCoreApp;
using YL.Utils.Pub;
using YL.Utils.Table;

using IServices;
using Microsoft.AspNetCore.Authorization;
using System.Collections.Generic;
using System.Threading.Tasks;
using YL.Core.Dto;

namespace EduCMS.Controllers
{
    [Route("[controller]")]
    public class NotificationController : BaseController
    {
        private readonly IWhr_notificationServices _services;
        private readonly IWhr_notification_categoryServices _categoryServices;
        
        public NotificationController(IWhr_notificationServices services, IWhr_notification_categoryServices categoryServices) 
        {
            _services = services;
            _categoryServices = categoryServices;
        }

        /// <summary>
        /// 类型列表
        /// </summary>
        /// <returns></returns>
        // GET: /notification/categories/
        [HttpGet("categories")]
        public JsonResult Categories()
        {
            var categories = _categoryServices.GetAll(ifPatriarch: 1);
            return Json(Microprogram.DataInfo(PubEnum.Success, "", categories));
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="categoryId"></param>
        /// <param name="bootstrapParams"></param>
        /// <returns></returns>
        // GET: /notification/categories/{categoryId}
        [HttpPost("Categories/{categoryId}")]
        public JsonResult Get(int categoryId, [FromForm] Bootstrap.BootstrapParams bootstrapParams)
        {
            var result = _services.PageList(UserDto.UserId, categoryId, bootstrapParams);
            return Json(Microprogram.DataInfo(PubEnum.Success, "", Bootstrap.GridData(result.Item2, result.Item1)));
        }

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        // GET: /notification/{id}
        [HttpGet("{id}")]
        public JsonResult Detail(int id)
        {
            var notification = _services.Find(UserDto.UserId, id);
            return Json(Microprogram.DataInfo(PubEnum.Success, "", notification));
        }

        /// <summary>
        /// 我已阅读
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        // PUT: /notification/5
        [HttpGet("{id}")]
        public JsonResult Read(int id)
        {
            _services.Read(UserDto.UserId, id);
            return Json(Microprogram.DataInfo(PubEnum.Success, "", null));
        }
    }
}